package com.jackeysun.util

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration


/**
 * Created by sunxunchao on 2021/8/16.
 */
class DividerItemDecoration(
    var spanCount: Int = 1, //列数
    var spacing: Int = 0, //间隔
    var includeEdge: Boolean = false, //是否包含边缘
    var spaceColor: Int = Color.TRANSPARENT,
) : ItemDecoration() {

    private var bounds = Rect()
    private var dividerDrawable: Drawable = ColorDrawable(spaceColor)


    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State,
    ) {
        if (parent.layoutManager is LinearLayoutManager) {
            val position = parent.getChildAdapterPosition(view) // item position
            when ((parent.layoutManager as LinearLayoutManager).orientation) {
                RecyclerView.VERTICAL -> {
                    val column = position % spanCount // item column
                    if (includeEdge) {
                        outRect.left =
                            spacing - column * spacing / spanCount // spacing - column * ((1f / spanCount) * spacing)
                        outRect.right =
                            (column + 1) * spacing / spanCount // (column + 1) * ((1f / spanCount) * spacing)
                        if (position < spanCount) { // top edge
                            outRect.top = spacing
                        }
                        outRect.bottom = spacing // item bottom
                    } else {
                        outRect.left =
                            column * spacing / spanCount // column * ((1f / spanCount) * spacing)
                        outRect.right =
                            spacing - (column + 1) * spacing / spanCount // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                        if (position >= spanCount) {
                            outRect.top = spacing // item top
                        }
                    }
                }
                RecyclerView.HORIZONTAL -> {
                    val row = position % spanCount // item row
                    if (includeEdge) {
                        outRect.top =
                            spacing - row * spacing / spanCount // spacing - row * ((1f / spanCount) * spacing)
                        outRect.bottom =
                            (row + 1) * spacing / spanCount // (row + 1) * ((1f / spanCount) * spacing)
                        if (position < spanCount) { // top edge
                            outRect.left = spacing
                        }
                        outRect.right = spacing // item bottom
                    } else {
                        outRect.top =
                            row * spacing / spanCount // row * ((1f / spanCount) * spacing)
                        outRect.bottom =
                            spacing - (row + 1) * spacing / spanCount // spacing - (row + 1) * ((1f /    spanCount) * spacing)
                        if (position >= spanCount) {
                            outRect.left = spacing // item top
                        }
                    }
                }
            }
            bounds = outRect
        } else {
            super.getItemOffsets(outRect, view, parent, state)
        }


    }

    /**
     * 间距内
     */
    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)

        val itemCount = parent.childCount
        for (i in 0 until itemCount) {
            val child = parent.getChildAt(i)
            parent.getDecoratedBoundsWithMargins(child, bounds)
            dividerDrawable.bounds = bounds
            dividerDrawable.draw(c)
        }
    }

    /**
     * item
     */
    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
    }
}